import { createRouter, createWebHistory } from 'vue-router'
import Login from '@/views/Login/index.vue'
import Home from '@/views/Home/index.vue'
import Layout from '@/views/Layout/index.vue'
import Demo from '@/views/Demo/index.vue'
import Category from '@/views/Category/index.vue'
import SubCategory from '@/views/SubCategory/index.vue'
import GoodsDetail from '@/views/GoodsDetail/index.vue'
import CartList from '@/views/Cart/CartList.vue'
import CheckOut from '@/views/CheckOut/index.vue'
import Pay from '@/views/Pay/index.vue'
import Member from '@/views/Member/index.vue'
import UserInfo from '@/views/Member/components/UserInfo.vue'
import UserOrder from '@/views/Member/components/UserOrder.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'layout',
      component: Layout,
      redirect: '/',
      children: [
        {
          path: '',
          name: 'home',
          component: Home,
        },
        {
          path: 'category/:id',
          name: 'category',
          component: Category,
        },
        {
          path: 'category/sub/:id',
          name: 'subCategory',
          component: SubCategory,
        },
        {
          path: 'goods/detail/:id',
          name: 'goodsDetail',
          component: GoodsDetail,
        },
        {
          path: 'cart/list',
          name: 'cartList',
          component: CartList,
        },
        {
          path: 'checkout',
          name: 'checkout',
          component: CheckOut,
        },
        {
          path: 'pay/:id',
          name: 'pay',
          component: Pay,
        },
        {
          path: 'member',
          name: 'member',
          component: Member,
          redirect: '/member/user',
          children: [
            {
              path: 'user',
              name: 'user',
              component: UserInfo,
            },
            {
              path: 'order',
              name: 'order',
              component: UserOrder,
            },
          ]
        },

      ],
    },
    {
      path: '/login',
      name: 'login',
      component: Login,
    },
    {
      path: '/demo',
      name: 'demo',
      component: Demo,
    },
  ],
  scrollBehavior() {
    return { top: 0 }
  },
})
router.beforeEach((to, from, next) => {
  // to 就是目标路由对象
  if (to.meta.requiresAuth) {
    router.push('/login')
    router.push('/login')
    return
  }
  next()
})
export default router
